package com.flyme.module.base.provider.service.task;


import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.module.base.client.entity.SysConfig;
import com.flyme.module.base.provider.nuonuo.oauth.Oauth2AccessToken;
import com.flyme.module.base.provider.service.SysConfigService;
import com.flyme.module.base.provider.service.SysUsercertificateService;
import com.flyme.module.base.provider.service.SysUserexaminationService;
import com.flyme.module.file.provider.utils.TestStsService;
import com.flyme.module.order.provider.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Configurable
@EnableScheduling
@PropertySource("classpath:application.yml")
public class AliVodTask {
    @Autowired
    private SysConfigService configService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private SysUserexaminationService userexaminationService;
    @Autowired
    private SysUsercertificateService usercertificateService;

    @Value("${spring.profiles.active}")
    public String active;

    /**
     * 定时刷新点播
     */
    @Scheduled(initialDelay = 5000, fixedRate = 3000000)// 间隔50更新一次
    public void myTest2() throws ClientException {
        if("dev".equals(active)){
            return ;
        }
        AssumeRoleResponse response = TestStsService.assumeRole();
        configService.update(new UpdateWrapper<SysConfig>().set("configVal", response.getCredentials().getAccessKeyId()).eq("configName", "AccessKeyId"));
        configService.update(new UpdateWrapper<SysConfig>().set("configVal", response.getCredentials().getAccessKeySecret()).eq("configName", "AccessKeySecret"));
        configService.update(new UpdateWrapper<SysConfig>().set("configVal", response.getCredentials().getSecurityToken()).eq("configName", "SecurityToken"));
    }

    /**
     * 定时更新订单状态
     */
    @Scheduled(initialDelay = 12000, fixedRate = 60000)// 间隔1分钟更新一次
    public void orderJob() {
        if("dev".equals(active)){
            return ;
        }
        //orderService.orderJob();
        userexaminationService.orderJobCheck();
    }

    /**
     * 发票token定时刷新
     */
    @Scheduled(initialDelay = 22000, fixedRate = 82800000)// 间隔23小时更新一次
    public void nuoJob() throws ClientException {
        if("dev".equals(active)){
            return ;
        }
        String resValue = Oauth2AccessToken.getAccessToken(null, null);
        Map<String, Object> map = JSONUtil.toBean(JSONUtil.parseObj(resValue), Map.class, true);
        configService.update(new UpdateWrapper<SysConfig>().set("configVal", map.get("access_token")).set("updateTime", DateUtil.date().toString()).eq("configName", "nuoToken"));
    }

    /**
     * 定时更新订单状态
     */
    @Scheduled(cron="5 5 0 1 1 ? ")// 每年一月一号0点5分执行
    public void certificateJob() {
        if("dev".equals(active)){
            return ;
        }
        usercertificateService.certificateJob();
    }

}
